/**
 * Created by Delicate on 2017/5/31.
 */
let laypage, fairList;
require(["jquery", "common", "laypage","layer"], (a, b, c,d) => {
    laypage = c;
    init();
    monitor();
    service();
    page();
    layer=d;
})
const init = () => {
    $("#province").trigger("select");
    load(1);
}
const monitor = () => {
    $(".m-select-search").on("click",res=>{
        load(1);
    })
}
const service = () => {

}
const load = (number)=>{
    $.ajax({
        type:"post",
        url:"/platform/fair/list",
        datatype:"json",
        data:{
            pageNum: number,
            pageSize: 12,
            nickName: $("#nickName").val(),
            channel: $("#channel").val(),
            city: $("#city").val(),
            province: $("#province").val(),
            platform: $("#platform").val()
        },
        success:function(data){
            let _html = "";
            if(data.list.length>0){
                for (var i=0;i<data.list.length;i++){
                    const item = data.list[i];
                    _html += '<li class="m-choice-cell" style="height:215px;" onclick="cellClick(this)" data-id="'+item.id+'" data-head="'+item.photo+'" data-name="'+item.nick_name+'"> ' +
                        '<div class="u-cell-item-head"> ' +
                        '<div class="u-item-head" style="background: url('+item.photo+') center/cover no-repeat"></div> ' +
                        '<div class="u-item-name" style="font-size:16px;margin-top:10px;">'+item.nick_name+'</div> ' +
                        '<div class="u-item-summary">'+item.attrString+'</div> ' +
                        '</div> ' +
                        '<div class="u-cell-item-number"> ' +
                        '<div class="u-item-yxl"> ' +
                        '<div class="u-item-icon"></div> ' +
                        '<div class="u-item-text">点击数'+item.click+'</div> ' +
                        '</div> ' +
                        '<div class="u-item-rate"> ' +
                        '<div class="u-item-icon" style="background: url(/assets/front/web/resources/icon-zh.png) center/contain no-repeat;"></div> ' +
                        '<div class="u-item-text">转化数'+item.cvs+'</div> ' +
                        '</div> ' +
                        '</div> ' +
                        '</li>';
                }
                page(data.totalPage,data.pageNumber);
                $("#m_choice_page").show();
                
            }else {
//            	layer.alert("没有检索到任何相关信息");
            	_html+='<div style="margin:0 auto;padding:15px;color:red">暂时没有相关数据信息</div>'
            	$("#m_choice_page").hide();
            }
            $(".m-choice-list").html(_html);
            
        }
    })
}

const page = (total,index)=>{
    laypage({
        cont: 'm_choice_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: total?total:1, //通过后台拿到的总页数
        curr: index || 1, //当前页
        jump: function(obj, first){ //触发分页后的回调
            if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                load(obj.curr);
            }
        }
    });
}

//选择省 生成对应市
var changeRegion = obj => {
    var val = $(obj).val();
    if(val==""){
    	$("#city").empty();
        $("#city").append("<option value=''>请选择</option>")
        return;
    }
    
    $.ajax({
        type: "post",
        url: ctx + "/platform/fair/region",
        datatype: "json",
        data: {"id": val},
        success: function (data) {
            if (data.length > 0) {
                $("#city").empty();
                $("#city").append("<option value=''>请选择</option>")
                for (var i = 0; i < data.length; i++) {
                    $("#city").append("<option value=" + data[i].REGION_ID + " class=''>" + data[i].REGION_NAME + "</option>")
                }
            }
        }
    })

}

var cellClick = obj =>{
    const id = $(obj).data('id');
    window.location.href = "/platform/fair/detail?id="+id;
}

var clearSearch=function(){
	$("#nickName").val("");
}